package t4invoke;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import t4bean.FruitBean;
import t4bean.MoneyRecordBean;
import t4dao.FruitDao;
import t4dao.MoneyRecordDao;

public class ThreeAction implements Action {
	@Override
	public void three() {
		FruitDao fd = new FruitDao();
		List<FruitBean> list = null;
		try {
			list = fd.select(null);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		MoneyRecordDao mrd = new MoneyRecordDao();
		MoneyRecordBean mrb = new MoneyRecordBean();
		Integer number = 0;
		List<Integer> numberList = new ArrayList<>();
		List<MoneyRecordBean> moneyList = null;
		
		for (FruitBean fruit : list) {
			mrb.setFruitName(fruit.getName());
			try {
				moneyList = mrd.select(mrb);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			for (MoneyRecordBean mrb1 : moneyList) {
				number += mrb1.getNumber();
			}
			numberList.add(number);
			number = 0;
		}
		
		sort(numberList, list);
		for (int i = 0; i < 3; i++) {
			System.out.println("第" + (i + 1) + "个水果为" + list.get(i).getName());
		}
		System.out.println();
	}

	void sort(List<Integer> numberList, List<FruitBean> list) {
		FruitBean targer;
		Integer number2;
		for (int i = 1; i < list.size(); ++i) {
			for (int j = 0; j < i; ++j) {
				if (numberList.get(i).compareTo(numberList.get(j)) > 0) {
					targer = list.get(i);
					number2 = numberList.get(i);
					for (int k = i; k >= j + 1; --k) {
						list.set(k, list.get(k - 1));
						numberList.set(k, numberList.get(k - 1));
					}
					list.set(j, targer);
					numberList.set(j, number2);
				}
			}
		}
	}

	@Override
	public void sell(Scanner sc) {
		// TODO Auto-generated method stub

	}

	@Override
	public void purchase(Scanner sc) {
		// TODO Auto-generated method stub

	}

	@Override
	public void moneyGet(Scanner sc) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showAllSellRecord() {
		// TODO Auto-generated method stub

	}

	@Override
	public void showFruit() {
		// TODO Auto-generated method stub

	}

}
